﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Browser;

namespace MenabehShowingMessage
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            
        }

        Thread thShowTime = null;


        Thread doJob = null;

        protected void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            thShowTime = new Thread(fnShowTime);
            thShowTime.Start();
            
        }

        delegate void delShowTime();

        void fnShowTimeInvoker()
        {
            lblCurrentTime.Content = DateTime.Now.ToString("hh:mm:ss tt");
        }


        void fnShowTime()
        {
            while (true)
            {
                Thread.Sleep(500);

                this.Dispatcher.BeginInvoke(new delShowTime(fnShowTimeInvoker));
            }
        }

        

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (rdDuration.IsChecked.Value)
            {
                try
                {
                    TimeSpan ts = new TimeSpan(int.Parse(txtHour.Text), int.Parse(txtMinute.Text), int.Parse(txtSecound.Text));
                    this.ts = ts;

                    

                    doJob = new Thread(startCount);
                    doJob.Start();
                    
                }
                catch
                {
                    MessageBox.Show("خطأ , من فضلك حدد مدة زمنية سليمة");
                }
            }
            else
            {
                try
                {
                    DateTime dtNew = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, int.Parse(txtHour.Text), int.Parse(txtMinute.Text), int.Parse(txtSecound.Text));

                    this.dtNew = dtNew;


                    

                    doJob = new Thread(startMenabeh);
                    doJob.Start();
                    
                }
                catch
                {
                    MessageBox.Show("خطأ , من فضلك حدد زمن سليم");
                }
            }
             
        }


        DateTime dtNew = DateTime.Now;
        private void startMenabeh()
        {
            this.Dispatcher.BeginInvoke(new Action(disableMenabehControls));
            //disableMenabehControls();

            while (DateTime.Now < dtNew)
            {
                if (requestCancel)
                    break;
                Thread.Sleep(500);
            }

            if (!requestCancel)
                this.Dispatcher.BeginInvoke(new Action(doEvent));

            requestCancel = false;
            //doEvent();


            this.Dispatcher.BeginInvoke(new Action(enableMenabehControls));
            //enableMenabehControls();
            
        }

        private void doEvent()
        {
            try
            {
                try
                {
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                    
                    Application.Current.MainWindow.TopMost = true;
                    Application.Current.MainWindow.Activate();



                }
                catch
                {
                    
                }


                try
                {
                    ScriptObject screen = (ScriptObject)HtmlPage.Window.GetProperty("screen");
                    int height = int.Parse(screen.GetProperty("height").ToString());
                    int width = int.Parse(screen.GetProperty("width").ToString());


                    HtmlPage.Window.Invoke("moveTo", 0, 0);
                    HtmlPage.Window.Invoke("resizeTo", width, height);
                }
                catch
                {
                }




                try
                {
                    Random rand = new Random((int)DateTime.Now.Ticks);
                    int music = rand.Next(7) + 1;
                    string fileName = music.ToString() + ".mp3";
                    mePlay.Source =  new Uri(fileName,UriKind.Relative);
                    mePlay.Volume = 100;
                    
                    mePlay.Play();
                    
                }
                catch
                {
                }
                      
                
                

                this.Focus();
                //MessageBox.Show("مبروك , انتهى الوقت !");
            }
            catch(Exception ex)
            {
                MessageBox.Show("خطأ في البرنامج : \r\n" +   ex.ToString());
            }
        }

        protected void mePlay_MediaEnded(object sender, RoutedEventArgs e)
        {
            try
            {
                mePlay.Position = new TimeSpan(0);
                mePlay.Play();
            }
            catch
            {
            }
        }

        private void enableMenabehControls()
        {
            btnStart.IsEnabled = true;
            btnPause.IsEnabled = true;
            txtHour.IsEnabled = txtMinute.IsEnabled = txtSecound.IsEnabled = true;
            rdDuration.IsEnabled = rdTime.IsEnabled = true;
        }

        private void disableMenabehControls()
        {
            btnStart.IsEnabled = false;
            btnPause.IsEnabled = false;
            txtHour.IsEnabled = txtMinute.IsEnabled = txtSecound.IsEnabled = false;
            rdDuration.IsEnabled = rdTime.IsEnabled = false;
        }


        private void disableCounterControls()
        {
            btnStart.IsEnabled = false;
            
            txtHour.IsEnabled = txtMinute.IsEnabled = txtSecound.IsEnabled = false;
            rdDuration.IsEnabled = rdTime.IsEnabled = false;
        }


        private void enableCounterControls()
        {
            btnStart.IsEnabled = true;

            txtHour.IsEnabled = txtMinute.IsEnabled = txtSecound.IsEnabled = true;
            rdDuration.IsEnabled = rdTime.IsEnabled = true;
        }



        TimeSpan ts = new TimeSpan ();
        
        private void startCount()
        {
            this.Dispatcher.BeginInvoke(new Action(disableCounterControls));
            
            //disableCounterControls();



            
            DateTime dtEnd = DateTime.Now;



            while (true )
            {
                if (requestCancel)
                    break;

                if (!paused)
                {

                    //ts = new TimeSpan(int.Parse(txtHour.Text), int.Parse(txtMinute.Text), int.Parse(txtSecound.Text));

                    dtEnd = DateTime.Now + ts;

                    Thread.Sleep(500);

                    ts = dtEnd - DateTime.Now;



                    this.Dispatcher.BeginInvoke(new Action(bindCounterControls));
                    //bindCounterControls();

                    if (dtEnd < DateTime.Now)
                        break;
                }
            }

            if(!requestCancel)
                this.Dispatcher.BeginInvoke(new Action(doEvent));


            requestCancel = false;
            //doEvent();


            this.Dispatcher.BeginInvoke(new Action(enableCounterControls));
            //enableCounterControls();
        }

        private void bindCounterControls()
        {
            txtHour.Text = ts.Hours.ToString();
            txtMinute.Text = ts.Minutes.ToString();
            txtSecound.Text = ts.Seconds.ToString();
        }

        private void btnStopSound_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                mePlay.Stop();
            }
            catch
            {
            }
        }


        bool requestCancel = false;

        protected void btnStop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                requestCancel = true;
                
            }
            catch
            {
               
            }

           
        }


        bool paused = false;

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            if (paused)
            {
                paused = false;
                btnPause.Content = "إيقاف مؤقت";
            }
            else
            {
                paused = true;
                btnPause.Content = "إستمرار";
            }
        }
    }
}
